import ccclass = cc._decorator.ccclass;
import property = cc._decorator.property;
import { wx } from "./Wx";



@ccclass
export class SubContextView extends cc.Component {
    private sprite: cc.Sprite = null;
    private readonly texture: cc.Texture2D = new cc.Texture2D();

    // 用于表示当前子域需要绘制哪个View.
    @property
    private viewName: string = "";

    // 约定要获取的key的名称.即 getCloudStorage(keyList:key)
    @property
    private key: string = "";

    // 用于表示子域数据根据什么key 进行排行.
    @property
    private sortKey: string = "";

    @property({
        tooltip: "item之间的y间隔"
    })
    spacingY: number = 5;

    public onLoad() {
        this.sprite = this.getComponent(cc.Sprite);
        if (!this.sprite) {
            this.sprite = this.addComponent(cc.Sprite);
        }
    }

    public onEnable() {
        if (!this.isWechat()) {
            return;
        }

        if (this.viewName.length <= 0 || this.key.length <= 0) {
            console.error("SubContextView 未设置子域viewName 和key ,无法显示子域");
            return;
        }
        console.log("SubContextView 显示子域:", this.viewName, this.key);
        const shareCanvas = this.getShareCanvas();
        shareCanvas.width = this.node.width;
        shareCanvas.height = this.node.height;

        let bound: any = {};

        bound.width = this.node.getContentSize().width;
        bound.height = this.node.getContentSize().height;

        let phoneInfo = wx.getSystemInfoSync();
        console.log("phoneInfo:", phoneInfo);

        let winSize = cc.winSize;

        let scaleX = phoneInfo.screenWidth / winSize.width
        let scaleY = phoneInfo.screenHeight / winSize.height

        //微信像素
        let dX = winSize.width / 2 - (this.node.width / 2 - this.node.x);
        let xStart = dX * scaleX;

        let dY = winSize.height / 2 - this.node.height / 2 - this.node.y;
        let yStart = dY * scaleY;

        let startPos = {
            x: xStart,
            y: yStart
        };

        this.sendMessageToSub("action_init_context", { view: this.viewName, bound: bound, spacingY: this.spacingY, scaleX: scaleX, scaleY: scaleY, startPos: startPos });

        this.updateView();
    }

    public updateView() {
        console.log("更新updateView 更新updateView 更新updateView");
        this.sendMessageToSub(this.viewName, { sortKey: this.sortKey, key: this.key });
    }

    public setPage(page: number) {
        this.sendMessageToSub("action_paging", { page: page, view: this.viewName });
    }

    public nextPage() {
        console.log("下一页");
        this.sendMessageToSub("action_paging", { add: 1, view: this.viewName });
    }

    public lastPage() {
        console.log("上一页");
        this.sendMessageToSub("action_paging", { add: -1, view: this.viewName });
    }


    public lateUpdate() {
        this.render();
    }

    private render() {
        if (!this.isWechat()) {
            return;
        }
        const shareCanvas = this.getShareCanvas();
        this.texture.initWithElement(shareCanvas);
        this.texture.handleLoadedTexture();
        if (!this.sprite.spriteFrame) {
            this.sprite.spriteFrame = new cc.SpriteFrame(this.texture);
        } else {
            this.sprite.spriteFrame.setTexture(this.texture);
        }
    }

    private isWechat(): boolean {
        return cc.sys.platform === cc.sys.WECHAT_GAME;
    }

    private sendMessageToSub(operation: string, params: any) {
        if (!this.isWechat()) {
            return;
        }
        let openDataContext = wx.getOpenDataContext();
        if (openDataContext) {
            let data: any = {};
            data.what = operation;
            data.arguments = params;
            openDataContext.postMessage(data);
        }
    }

    private getShareCanvas() {
        if (!this.isWechat()) {
            return;
        }
        let openDataContext = wx.getOpenDataContext();
        if (openDataContext) {
            return openDataContext.canvas;
        }

    }

}
